package com.heartunderblade.rpc.proxy;

import com.heartunderblade.rpc.enumDefault.DefaultValue;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MockServiceProxy implements InvocationHandler {
    /**
     * 模拟对象，在服务尚未开发完全时。
     * @param proxy the proxy instance that the method was invoked on
     *
     * @param method the {@code Method} instance corresponding to
     * the interface method invoked on the proxy instance.  The declaring
     * class of the {@code Method} object will be the interface that
     * the method was declared in, which may be a superinterface of the
     * proxy interface that the proxy class inherits the method through.
     *
     * @param args an array of objects containing the values of the
     * arguments passed in the method invocation on the proxy instance,
     * or {@code null} if interface method takes no arguments.
     * Arguments of primitive types are wrapped in instances of the
     * appropriate primitive wrapper class, such as
     * {@code java.lang.Integer} or {@code java.lang.Boolean}.
     *
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> returnType = method.getReturnType();
        System.out.println("Method invoke "+ method.getName() );
        return getDefalutObject(returnType);
    }

    private Object getDefalutObject(Class<?> returnType) {
        // Check for primitive types and return their corresponding default values
        if (returnType.isPrimitive()) {
            if (returnType == boolean.class) {
                return DefaultValue.Boolean.getValue();
            } else if (returnType == short.class) {
                return DefaultValue.Short.getValue();
            } else if (returnType == int.class) {
                return DefaultValue.Int.getValue();
            } else if (returnType == long.class) {
                return DefaultValue.Long.getValue();
            } else if (returnType == String.class) {
                return DefaultValue.String.getValue();
            }
        }

        // If not a primitive type, return the default Object value
        return DefaultValue.Object.getValue();
    }
}
